<?php

class Agen extends CI_Controller {

    var $_public_view;

    function __construct() {
        parent::__construct();
        $this->load->model('model_agen', 'magen');
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->library('pagination');
        $this->config->load('artyproperty');
        $this->_public_view = $this->config->item('public_view');
        $this->session->set_userdata('page', 4);
    }

    function index() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $data = array('daftar' => $this->magen->get_agen(),
            'page_content' => 'view_agen',
            'page_title' => 'Daftar Agen',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

    function detail() {
        $str_id = '';
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $this->urutan = $this->input->get('urutan', TRUE);
        $this->nitem = $this->input->get('nitem', TRUE);
        if ($this->nitem == "") {
            $this->nitem = "10";
        }

        if ($this->uri->segment(3) == FALSE) {
            redirect('agen', 'location');
        } else {
            $str_id = $this->uri->segment(3);
        }
        if ($this->uri->segment(4) == FALSE) {
            $page = 1;
        } else {
            $page = $this->uri->segment(4);
        }
        $sql = "SELECT * FROM
            properti AS p, tipe_properti AS t, furnitur AS f,
            sertifikasi AS s
            WHERE p.user_id='" . $str_id . "'
                    AND p.tipe = t.tipe
                    AND p.furnitur = f.furnitur
                    AND p.sertifikasi = s.sertifikasi
                     ";
        if ($this->urutan == "6") {
            $sql = $sql . "ORDER BY luas_bangunan ASC";
        } else if ($this->urutan == "5") {
            $sql = $sql . "ORDER BY luas_bangunan DESC";
        } else if ($this->urutan == "4") {
            $sql = $sql . "ORDER BY harga DESC";
        } else if ($this->urutan == "3") {
            $sql = $sql . "ORDER BY harga ASC";
        } else if ($this->urutan == "2") {
            $sql = $sql . "";
        } else {
            $sql = $sql . "ORDER BY tgl_update DESC";
        }
        $query = $this->db->query($sql);
        $result = array_slice($query->result(), ($page - 1) * $this->nitem, $this->nitem);
        $total_rows = $query->num_rows();

        if (count($_GET) > 0)
            $config['suffix'] = '?' . http_build_query($_GET, '', "&");
        $config['base_url'] = site_url("agen/detail/$str_id/");
        $config['first_url'] = $config['base_url'] . '?' . http_build_query($_GET);
        $config['uri_segment'] = 4;
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $this->nitem;
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 10;
        $config['first_link'] = 'Pertama';
        $config['last_link'] = 'Terakhir';
        $config['next_link'] = 'Berikutnya';
        $config['prev_link'] = 'Sebelumnya';
        $this->pagination->initialize($config);

        $sql = "SELECT COUNT(*) as jual FROM properti WHERE user_id='" . $str_id . "' AND sewa_beli='1'";
        $query = $this->db->query($sql);
        $tmp = $query->result();
        $jual = $tmp[0]->jual;

        $sql = "SELECT COUNT(*) as sewa FROM properti WHERE user_id='" . $str_id . "' AND sewa_beli='2'";
        $query = $this->db->query($sql);
        $tmp = $query->result();
        $sewa = $tmp[0]->sewa;

        $sql = "SELECT * FROM user AS u, perusahaan AS p, alamat AS a WHERE user_id='" . $str_id . "' AND u.perusahaan_id = p.perusahaan_id AND p.alamat_id = a.alamat_id";
        $query = $this->db->query($sql);
        $agen = $query->result();

        $detail = $this->magen->get_user_detail($str_id);
        if ($detail) {
            $data = array('detail' => $detail,
                'page_content' => 'detail_agen',
                'page_title' => 'Detail Agen',
                'isLogin' => $islogin,
                'user' => $user,
                'result' => $result,
                'agen' => $agen,
                'jual' => $jual,
                'sewa' => $sewa);
            $this->load->view($this->_public_view, $data);
        } else {
            redirect('home', 'location');
        }
    }

    function cari($page = 1) {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $kategori = $this->input->get('kategori', TRUE);
        $search = $this->input->get('search', TRUE);
        $urutan = $this->input->get('urutan', TRUE);
        $nitem = $this->input->get('nitem', TRUE);
        if ($nitem == "") {
            $nitem = 10;
        }
        $sql = "SELECT * FROM (
            SELECT user.*, COUNT(properti_id) as count
            FROM user
            LEFT OUTER JOIN properti
            ON user.user_id = properti.user_id
            GROUP BY user.user_id
            ) AS user,
            perusahaan, alamat
            WHERE user.perusahaan_id = perusahaan.perusahaan_id
            AND perusahaan.alamat_id = alamat.alamat_id ";
        if ($kategori == "1") {
            $sql = $sql . "AND nama_perusahaan LIKE '" . $search . "%'";
        } else if ($kategori == "2") {
            $sql = $sql . "AND (nama_depan LIKE '%" . $search . "%' OR nama_belakang LIKE '%" . $search . "%')";
        } else {
            //$sql = $sql . "AND (nama_depan LIKE '%" . $search . "%' OR nama_belakang LIKE '%" . $search . "%')";
        }
        if ($urutan == "4") {
            
        } else if ($urutan == "3") {
            $sql = $sql . "ORDER BY count DESC";
        } else if ($urutan == "2") {
            $sql = $sql . "ORDER BY nama_depan DESC";
        } else {
            $sql = $sql . "ORDER BY nama_depan ASC";
        }
        $query = $this->db->query($sql);
        $result = array_slice($query->result(), ($page - 1) * $nitem, $nitem);

        if (count($_GET) > 0)
            $config['suffix'] = '?' . http_build_query($_GET, '', "&");
        $config['base_url'] = site_url('agen/cari/');
        $config['first_url'] = $config['base_url'] . '?' . http_build_query($_GET);
        $config['total_rows'] = $query->num_rows();
        $config['per_page'] = $nitem;
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 10;
        $config['first_link'] = 'Pertama';
        $config['last_link'] = 'Terakhir';
        $config['next_link'] = 'Berikutnya';
        $config['prev_link'] = 'Sebelumnya';
        $this->pagination->initialize($config);

        $data = array(
            'page_content' => 'cari_agen',
            'page_title' => 'Cari Agen',
            'isLogin' => $islogin,
            'user' => $user,
            'result' => $result,
            'page' => $page,
            'nitem' => $nitem,
            'total' => $query->num_rows);
        $this->load->view($this->_public_view, $data);
    }

}

?>
